package io.bluerain.ntry;

import java.io.*;

/**
 * Created by foredawn on 15-8-26.
 * java7中的try-with-resources语法
 */
public class TryWithResources {

    public static void main(String[] args) {
        System.out.println(readText());
        testDefinedResourceOpe();
    }


    public static String readText() {
        StringBuilder sb = new StringBuilder();
        //将需要释放的资源申请放到try后面的圆括号中
        try (BufferedReader reader = new BufferedReader(
                new FileReader(TryWithResources.class.getClassLoader().getResource("try-with-resources").getFile()))) {

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append(String.format("%n"));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static void testDefinedResourceOpe() {
        try (DefinedResourceOpe ope = new DefinedResourceOpe()) {
            ope.use();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
